//
// Created by zolobdz on 2018/2/1.
// Copyright (c) 2018 HeChao. All rights reserved.
//

import Foundation
import SnapKit
import Rswift

class CDNormalAlertView: UIView{

    lazy var containerView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white
        view.layer.cornerRadius = 15.0
        view.clipsToBounds = true
        return view
    }()

    lazy var iconImageView: UIImageView = {
        let view = UIImageView()
//        view.image = UIImage(resource: R.image.app_update)
        return view
    }()

    lazy var titleLabel: UILabel = {
        let view = UILabel()
        view.font = UIFont.boldSystemFont(ofSize: 18.0)
        view.text = ""
        return view
    }()

    lazy var textLabel: UITextView = {
        let view = UITextView()
        view.font = UIFont.systemFont(ofSize: 14.0)
        view.text = ""
        view.isEditable = false
//        view.numberOfLines = 0
        return view
    }()

    lazy var operationButton: UIButton = {
        let view = UIButton()
        view.setTitle("", for: .normal)
        view.setBackgroundImage(CDUtility.createImageWithColor(color: CDColor.themePink), for: .normal) // 为了添加点击效果
        view.layer.cornerRadius = 4.0
        view.layer.masksToBounds = true
        view.titleLabel?.font = CDActionButtonTextFont
        return view
    }()

    lazy var nextTimeButton: UIButton = {
        let view = UIButton()
        view.setTitle("", for: .normal)
        view.setBackgroundImage(CDUtility.createImageWithColor(color: UIColor.white), for: .normal) // 为了添加点击效果
        view.layer.cornerRadius = 4.0
        view.layer.masksToBounds = true
        view.setTitleColor(CDColor.themePink, for: .normal)
        view.titleLabel?.font = CDActionButtonTextFont
        view.isHidden = true
        return view
    }()

    lazy var closeButton: UIButton = {
        let view = UIButton()
//        view.setImage(UIImage(resource: R.image.guide_close), for: .normal)
        view.isHidden = true
        return view
    }()

    var operationClosure: (()->Void)?

    init() {
        super.init(frame: CGRect.zero)
        isHidden = true
        backgroundColor = UIColor.init(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.4)
        addSubview(containerView)
        containerView.addSubview(closeButton)
        containerView.addSubview(iconImageView)
        containerView.addSubview(titleLabel)
        containerView.addSubview(textLabel)
        containerView.addSubview(operationButton)
        containerView.addSubview(nextTimeButton)

        containerView.snp.remakeConstraints { (maker) in
            maker.center.equalToSuperview()
            maker.height.equalTo(370.0)
            maker.left.equalTo(32.0)
            maker.right.equalTo(-32.0)
        }

        nextTimeButton.snp.remakeConstraints { (maker) in
            maker.bottom.equalToSuperview().offset(-15.0)
            maker.height.equalTo(44.0)
            maker.left.equalTo(24.0)
            maker.right.equalTo(-24.0)
        }

        operationButton.snp.remakeConstraints { (maker) in
            maker.bottom.equalTo(nextTimeButton.snp.top).offset(-10.0)
            maker.height.equalTo(44.0)
            maker.left.equalTo(24.0)
            maker.right.equalTo(-24.0)
        }

        textLabel.snp.remakeConstraints { (maker) in
            maker.top.equalTo(containerView.snp.top).offset(161.0)
            maker.bottom.equalTo(operationButton.snp.top).offset(-15.0)
            maker.left.equalTo(24.0)
            maker.right.equalTo(-24.0)
        }

        titleLabel.snp.remakeConstraints { (maker) in
            maker.centerX.equalToSuperview()
            maker.top.equalTo(iconImageView.snp.bottom).offset(10.0)
        }

        iconImageView.snp.remakeConstraints { (maker) in
            maker.centerY.equalTo(containerView.snp.top).offset(81.0)
            maker.centerX.equalToSuperview()
        }

        closeButton.snp.remakeConstraints { (maker) in
            maker.right.equalTo(-15.0)
            maker.top.equalTo(13.0)
        }

        nextTimeButton.addTarget(self, action: #selector(closeAction), for: .touchUpInside)
        closeButton.addTarget(self, action: #selector(closeAction), for: .touchUpInside)
        operationButton.addTarget(self, action: #selector(publishDateButtonAction), for: .touchUpInside)
    }



    func config(_ type: CDNormalAlertType, titleText: String = "", message: String = "", closure: @escaping ()->Void) {
        operationClosure = closure
        titleLabel.isHidden = true
        nextTimeButton.isHidden = false
        closeButton.isHidden = false
        var messageText = ""
        var operationButtonText = ""
        var nextTimeButtonText = ""
//        switch type {
//        case .chargeVIP:
//            iconImageView.image = R.image.cd_alert_vip()
//            messageText = "您已经没有足够的糖币，充值会员可获取无限糖币并获得尊贵会员标识以及其它高级权益，大大增加您被异性用户青睐的几率！"
//            operationButtonText = "充值会员"
//            nextTimeButtonText = "暂不充值"
//        case .homepageOverLoad:
//            iconImageView.image = R.image.cd_alert_vip()
//            messageText = "您已达到今日加载上限，充值会员可提高加载上限并获得尊贵会员标识以及其它高级权益，大大增加您被异性用户青睐的几率！"
//            operationButtonText = "充值会员"
//            nextTimeButtonText = message
//        }

        let attributedString = NSMutableAttributedString(string: messageText)
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = 4.0
        attributedString.addAttributes([NSAttributedStringKey.paragraphStyle : paragraphStyle, NSAttributedStringKey.font : UIFont.systemFont(ofSize: 14.0)], range: NSMakeRange(0, messageText.count))
        textLabel.attributedText = attributedString

        operationButton.setTitle(operationButtonText, for: .normal)
        nextTimeButton.setTitle(nextTimeButtonText, for: .normal)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    @objc func closeAction() {
        isHidden = true
        if superview != nil{
            removeFromSuperview()
        }
    }

    @objc func publishDateButtonAction() {
        isHidden = true
        if superview != nil{
            removeFromSuperview()
        }
        if operationClosure != nil{
            operationClosure!()
        }
    }

}
